<?php

namespace App\Http\Controllers\admin;

use App\Http\Requests\RecommendCreateRequest;
use App\Http\Requests\RecommendUpdateRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Recommend;

class RecommendController extends Controller
{
    public function index(){
        return view('admin.recommend.index');
    }
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $model = Recommend::query();
        $recommend_word=$request->get('recommend_word');
        $search_word=$request->get('search_word');
        $status=$request->get('status');
        $start_time1=$request->get('start_time1');
        $start_time2=$request->get('start_time2');
        $end_time1=$request->get('end_time1');
        $end_time2=$request->get('end_time2');
        if(!empty($recommend_word)){
            $model = $model->where('recommend_word','like','%'.$recommend_word.'%');
        }
        if(!empty($search_word)){
            $model = $model->where('search_word','like','%'.$search_word.'%');
        }
        if(!empty($status)){
            switch ($status){
                case '1':
                    $model = $model->where('start_time','>',date('Y-m-d H:i:s'));
                    break;
                case '2':
                    $model = $model
                        ->where('start_time','<=',date('Y-m-d H:i:s'))
                        ->where('end_time','>',date('Y-m-d H:i:s'));
                    break;
                case '3':
                    $model = $model->where('end_time','<=',date('Y-m-d H:i:s'));
                    break;
                default:
            }
        }
        if(!empty($start_time1)){
            $model = $model->where('start_time','>=',$start_time1);
        }
        if(!empty($start_time2)){
            $model = $model->where('start_time','<=',$start_time2);
        }
        if(!empty($end_time1)){
            $model = $model->where('end_time','>=',$end_time1);
        }
        if(!empty($end_time2)){
            $model = $model->where('end_time','<=',$end_time2);
        }
        $res = $model->orderBy('id','desc')->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' =>$res['total'] ,
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
    public function create(){
        return view('admin.recommend.create');
    }
    /**
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function store(RecommendCreateRequest $request){
        $data = $request->only(['recommend_word','search_word','weight','start_time','end_time','remark']);
        if(strtotime($data['start_time']) >= strtotime($data['end_time']))
            return redirect()->to(route('admin.recommend.create'))->withErrors(['开始时间必须在结束时间之前']);
        if (Recommend::create($data)){
            return redirect()->to(route('admin.recommend'))->with(['status'=>'新增推荐词成功']);
        }
        return redirect()->to(route('admin.recommend'))->withErrors('系统错误');
    }

    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $recommend = Recommend::findOrFail($id);
        return view('admin.recommend.edit',compact('recommend'));
    }

    /**
     * @param Request $request
     * @param $id
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function update(RecommendUpdateRequest $request, $id)
    {
        $data = $request->only(['recommend_word','search_word','weight','start_time','end_time','remark']);
        $recommend = Recommend::findOrFail($id);
        if ($recommend->update($data)){
            return redirect(route('admin.recommend'))->with(['status'=>'更新成功']);
        }
        return redirect(route('admin.recommend'))->withErrors(['status'=>'系统错误']);
    }
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        if (Recommend::destroy($ids)){
            return response()->json(['code'=>0,'msg'=>'删除成功']);
        }
        return response()->json(['code'=>1,'msg'=>'删除失败']);
    }
}
